home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d17 / labels.arc / LABMULT.C < prev    next >
C/C++ Source or Header  |  1988-05-12  |  5KB  |  223 lines

  1. /* LABMULT - A Program to Produce Multiple Mailing Labels */
  2. /* Version 1.0 by Richard Conn */
  3.  
  4. /* Usage: labmult [-L#][-C#][-B#][-N#] filename.typ */
  5.  
  6. /* Instructions:
  7.    This program outputs the mailing labels from the indicated
  8.    file.  The number of labels to print is indicated by the
  9.    -N option, and the default value is 10.
  10.  
  11.    This program assumes a standard mailing label of 8 lines by 40
  12.    columns with a blank line between labels.  Lines beginning with
  13.    a double quote (") are interpreted literally and are output
  14.    without centering.  Lines beginning with a semicolon (;) are
  15.    treated as comments.  Blank lines are ignored.
  16.  
  17.    The options can be used to change the default values of the label
  18.    parameters.  The -L# sets the number of lines on the label,
  19.    -C# sets the number of columns, and -B# sets the number of blank
  20.    lines between labels.  The -N# sets the number of labels to something
  21.    other than the default.
  22.  
  23.    Diagnostics are recorded in the log file ERRLOG (defined below).
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. #define MAXLINES 20
  30. #define MAXCOLS  80
  31. #define NLINES 8
  32. #define NCOLS 40
  33. #define NBLANKS 1
  34. #define NLABS 10
  35. #define LINELEN 200
  36. #define ERRLOG "labmult.log"
  37. #define COMMENT ';'
  38. #define QUOTE '"'
  39.  
  40. char line[MAXLINES][MAXCOLS+2];
  41. FILE *fdo;
  42. int emptylabel;
  43. int labelcnt;
  44. int columns = NCOLS;
  45. int lines = NLINES;
  46. int blanks = NBLANKS;
  47. int labels = NLABS;
  48. int opterr = 0;
  49.  
  50. main (argc, argv)
  51. int argc;
  52. char **argv;
  53. {
  54.     char inline[LINELEN];
  55.         char *curchar;
  56.     int curline;
  57.         FILE *fd, *fopen();
  58.     int msgprinted = 0;
  59.     int i;
  60.     int fnindex = 0;
  61.     int exitcode;
  62.  
  63.     labelcnt = 0;
  64.     if (argc < 2) {
  65.         help (argv[0]);
  66.         exit (0);
  67.     }
  68.     for (i=1; i<argc; i++) {
  69.         switch (*argv[i]) {
  70.             case '-' : option (&argv[i][1]);
  71.                    break;
  72.             default  : fnindex = i;
  73.                    break;
  74.         }
  75.     }
  76.     if ((fnindex == 0) || (opterr)) {
  77.         help (argv[0]);
  78.         if (opterr > 1) {
  79.             switch (opterr) {
  80.                 case 2 : printf (" Max Columns Exceeded\n");
  81.                      break;
  82.                 case 3 : printf (" Max Lines Exceeded\n");
  83.                      break;
  84.                 case 4 : printf (" Label Count is Zero\n");
  85.                      break;
  86.                 default: break;
  87.             }
  88.         }
  89.         exit (0);
  90.     }
  91.     if ((fd = fopen (argv[fnindex], "r")) == NULL) {
  92.         printf (" Cannot open %s\n", argv[fnindex]);
  93.         exit (0);
  94.     }
  95.     if ((fdo = fopen (ERRLOG, "w")) == NULL) {
  96.         printf (" Cannot create log file %s\n", ERRLOG);
  97.         exit(0);
  98.     }
  99.     fprintf (fdo, " Label Parameters:");
  100.     fprintf (fdo, "  Lines = %d, Columns = %d, Blanks = %d\n",
  101.         lines, columns, blanks);
  102.     exitcode = 0;
  103.     while (fgets (inline, LINELEN, fd) != NULL) {
  104.         if ((inline[0] != COMMENT) && (inline[0] != '\n')) {
  105.             if (inline[0] > ' ') {
  106.                 if (exitcode) break;
  107.                 exitcode = 1;
  108.                 newlabel();
  109.                 msgprinted = 0;
  110.                 curline = -1;
  111.             }
  112.             curline++;
  113.             curchar = inline;
  114.             while ((*curchar == ' ') || (*curchar == '\t'))
  115.                 curchar++;
  116.             if (curline >= lines) {
  117.                 if (!msgprinted) {
  118.                 fprintf (fdo, " Number of Lines Exceeded\n");
  119.                 for (i=0; i<lines; i++)
  120.                     fprintf (fdo, "   %s", line[i]);
  121.                 msgprinted = 1;
  122.                 }
  123.             } else
  124.                 setline (curline, curchar);
  125.             }
  126.         }
  127.     fclose (fd);
  128.     for (i=0; i<labels; i++) printlabel ();
  129.     fprintf (fdo, " %d Labels Printed\n", labelcnt);
  130.     fclose (fdo);
  131. }
  132.  
  133. /* Print help message */
  134. help (str)
  135. char *str;
  136. {
  137.     printf (" Syntax: %s [-L#] [-C#] [-B#] [-N#] filename.typ\n",
  138.         str);
  139.     printf ("   Where:\n");
  140.     printf ("     -L# is the number of text lines on the label\n");
  141.     printf ("     -C# is the number of columns on the label\n");
  142.     printf ("     -B# is the number of blank lines between labels\n");
  143.     printf ("     -N# is the number of labels to print\n");
  144.     printf ("   Default values are L=%d, C=%d, B=%d, N=%d\n",
  145.         NLINES, NCOLS, NBLANKS, NLABS);
  146.     printf ("   Maximum values are L=%d, C=%d\n", MAXLINES, MAXCOLS);
  147. }
  148.  
  149. /* Process command-line options */
  150. option (str)
  151. char *str;
  152. {
  153.     switch (*str) {
  154.         case 'b' :
  155.         case 'B' : blanks = atoi (++str);
  156.                break;
  157.         case 'c' :
  158.         case 'C' : columns = atoi (++str);
  159.                if (columns > MAXCOLS) opterr = 2;
  160.                break;
  161.         case 'l' :
  162.         case 'L' : lines = atoi (++str);
  163.                if (lines > MAXLINES) opterr = 3;
  164.                break;
  165.         case 'n' :
  166.         case 'N' : labels = atoi (++str);
  167.                if (labels == 0) opterr = 4;
  168.                break;
  169.         default  : opterr = 1;
  170.                break;
  171.     }
  172. }
  173.  
  174. /* Store the next line into the label image */
  175. setline (number, inline)
  176. int number;
  177. char *inline;
  178. {
  179.     int spacein;
  180.         int i;
  181.  
  182.     if (inline[0] == QUOTE) {
  183.         strcpy (line[number], &inline[1]);
  184.     } else {
  185.         if (strlen(inline) > columns) {
  186.             spacein = 0;
  187.             fprintf (fdo, " Truncation of:\n    %s", inline);
  188.             inline[columns] = '\n';
  189.             inline[columns+1] = '\0';
  190.         } else
  191.             spacein = (columns - strlen(inline) - 1) / 2;
  192.         for (i=0; i<spacein; i++) line[number][i] = ' ';
  193.         strcpy (&line[number][spacein], inline);
  194.     }
  195.     emptylabel = 0;
  196. }
  197.  
  198. /* Initialize the label */
  199. newlabel ()
  200. {
  201.     int i;
  202.  
  203.     for (i=0; i<lines; i++) {
  204.         line[i][0] = '\n';
  205.         line[i][1] = '\0';
  206.     }
  207.     emptylabel = 1;
  208. }
  209.  
  210. /* Print the label */
  211. printlabel ()
  212. {
  213.     int i;
  214.  
  215.     if (emptylabel)
  216.         fprintf (fdo, " Attempt to print empty label\n");
  217.     else {
  218.         for (i=0; i<lines; i++) printf ("%s", line[i]);
  219.         for (i=0; i<blanks; i++) printf ("\n");
  220.         labelcnt++;
  221.     }
  222. }
  223.